﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;
using System.Drawing.Imaging;
using System.Runtime.InteropServices;

namespace THO7AlgoritmTimerApplication
{
    static class MyImage
    {
        public static int[] BmpToArray(Bitmap bitmap)
        {
            Rectangle rectangle = new Rectangle(0, 0, bitmap.Width, bitmap.Height);
            BitmapData bitmapData =
                bitmap.LockBits(rectangle, System.Drawing.Imaging.ImageLockMode.ReadWrite,
                bitmap.PixelFormat);
            IntPtr ptr = bitmapData.Scan0;
            int size = (bitmapData.Stride / 4) * bitmap.Height;

            int[] returnArray = new int[size];
            Marshal.Copy(ptr, returnArray, 0, size);
            bitmap.UnlockBits(bitmapData);
            return returnArray;
        }

        public static Bitmap ArrayToBmp(int[] array, Bitmap returnBitmap)
        {
            Rectangle rectangle = new Rectangle(0, 0, returnBitmap.Width, returnBitmap.Height);
            BitmapData bitmapData =
                returnBitmap.LockBits(rectangle, System.Drawing.Imaging.ImageLockMode.ReadWrite,
                returnBitmap.PixelFormat);
            IntPtr ptr = bitmapData.Scan0;
            int size = (bitmapData.Stride / 4) * returnBitmap.Height;

            Marshal.Copy(array, 0, ptr, size);
            returnBitmap.UnlockBits(bitmapData);
            return returnBitmap;
        }
    }
}






